package com.leo;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @ClassName RadioButtonDemo
 * @Description
 * @Author wangss
 * @date 2020.05.19 22:22
 * @Version 1.0
 */
public class RadioButtonDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #fff2f2");

        HBox hBox = new HBox(10d);

        ToggleGroup toggleGroup = new ToggleGroup();
        RadioButton radioButton1 = new RadioButton("r1");
        RadioButton radioButton2 = new RadioButton("r2");
        RadioButton radioButton3 = new RadioButton("r3");
        RadioButton radioButton4 = new RadioButton("r4");
        toggleGroup.getToggles().addAll(radioButton1, radioButton2, radioButton3, radioButton4);

        // 默认选择
        radioButton3.setSelected(true);
        toggleGroup.selectToggle(radioButton2);

        hBox.getChildren().addAll(radioButton1, radioButton2, radioButton3, radioButton4);

        AnchorPane.setTopAnchor(hBox, 200d);
        AnchorPane.setLeftAnchor(hBox, 100d);
        anchorPane.getChildren().addAll(hBox);
        Scene scene = new Scene(anchorPane);

        primaryStage.setScene(scene);
        primaryStage.setTitle("菜单");
        primaryStage.setWidth(500d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        // 监听
        radioButton1.selectedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

            }
        });

        toggleGroup.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {

            }
        });
    }
}
